package com.hongtu.message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hongtu.model.AccountChangeEvent;
import com.hongtu.service.AccountInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "topic_txmsg", consumerGroup = "consumer_txmsg_group_bank2")
@Slf4j
public class TxMsgConsumer implements RocketMQListener<String> {

    @Autowired
    AccountInfoService
            accountInfoService;

    @Override
    public void onMessage(String s) {
        log.info("开始消费消息:{}", s);
//解析消息为对象
        final JSONObject jsonObject = JSON.parseObject(s);
        AccountChangeEvent accountChangeEvent =
                JSONObject.parseObject(jsonObject.getString("accountChangeEvent"), AccountChangeEvent.class);
//调用service增加账号金额
        accountChangeEvent.setAccountNo("2");
        accountInfoService.addAccountInfoBalance(accountChangeEvent);
    }
}
